"""
模型路由文件
提供模型列表和模型信息的API接口
"""
from fastapi import APIRouter, HTTPException
from typing import List, Dict, Any

from .config import get_all_models, get_models_by_type, select_model

# 创建路由
router = APIRouter(
    prefix="/model",
    tags=["models"],
    responses={404: {"description": "Not found"}},
)

@router.get("/list")
async def get_model_list():
    """
    获取所有可用模型列表
    """
    models = get_all_models()
    return {
        "models": models,
        "count": len(models)
    }

@router.get("/type/{model_type}")
async def get_models_by_type_api(model_type: str):
    """
    根据类型获取模型列表
    
    - **model_type**: 模型类型，如 'gemini', 'gemma', 'embedding', 'text-embedding'
    """
    try:
        models = get_models_by_type(model_type)
        return {
            "type": model_type,
            "models": models,
            "count": len(models)
        }
    except ValueError as e:
        raise HTTPException(status_code=400, detail=str(e))

@router.get("/info/{model_name}")
async def get_model_info(model_name: str):
    """
    获取指定模型的详细信息
    
    - **model_name**: 模型名称
    """
    models = get_all_models()
    if model_name not in models:
        raise HTTPException(status_code=404, detail=f"Model {model_name} not found")
    
    # 确定模型类型
    model_type = None
    if model_name.startswith("gemini"):
        model_type = "gemini"
    elif model_name.startswith("gemma"):
        model_type = "gemma"
    elif model_name.startswith("embedding"):
        model_type = "embedding"
    elif model_name.startswith("text-embedding"):
        model_type = "text-embedding"
    
    # 返回模型信息
    model_info = {
        "id": model_name,
        "type": model_type,
        "is_default": model_name == select_model
    }
    
    # 添加Gemma模型的特定信息
    if model_type == "gemma":
        # 提取模型大小
        size = None
        if "2b" in model_name:
            size = "2B"
        elif "3b" in model_name:
            size = "3B"
        elif "7b" in model_name:
            size = "7B"
        elif "27b" in model_name:
            size = "27B"
        
        # 检查是否是指令微调版本
        instruction_tuned = "it" in model_name
        
        model_info.update({
            "size": size,
            "instruction_tuned": instruction_tuned
        })
    
    return model_info

@router.get("/current")
async def get_current_model():
    """
    获取当前选择的默认模型
    """
    return {
        "current_model": select_model
    } 